package com.mm.cloud.sys.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.cloud.common.core.enums.DeleteEnum;
import com.mm.cloud.common.core.exception.BaseException;
import com.mm.cloud.common.core.result.Result;
import com.mm.cloud.common.core.result.ResultConstants;
import com.mm.cloud.common.core.result.ResultUtil;
import com.mm.cloud.common.security.annotation.ParameterCheck;
import com.mm.cloud.common.security.loginuser.LoginUser;
import com.mm.cloud.common.security.helper.LoginUserHelper;
import com.mm.cloud.sys.dto.form.DictTypeDTO;
import com.mm.cloud.sys.dto.query.QueryDictTypeDTO;
import com.mm.cloud.sys.entity.DictType;
import com.mm.cloud.sys.mapper.DictTypeMapper;
import com.mm.cloud.sys.service.IDictTypeService;
import com.mm.cloud.sys.vo.DictTypeVO;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 字典类型 服务实现类
 * </p>
 *
 * @author mr
 * @since 2022-11-12
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements IDictTypeService {

    @Override
    public Result<List<DictTypeVO>> getDecTypeList(QueryDictTypeDTO dto) {
        IPage<DictTypeVO> page = this.baseMapper.getDecTypeList(new Page<>(dto.getPage(), dto.getLimit()), dto);
        return ResultUtil.success(page.getRecords(), page.getTotal());
    }

    @Override
    @ParameterCheck
    public Result<String> saveDicType(DictTypeDTO dto) {
        LoginUser loginUser = LoginUserHelper.getLoginUser();
        // 校验字典类型名称
        DictType existDictType = this.getOne(Wrappers.<DictType>lambdaQuery()
                .eq(DictType::getDictTypeName, dto.getDictTypeName())
                .eq(DictType::getIsDelete, DeleteEnum.NOT_DELETE.getStatus()));
        if(Objects.nonNull(existDictType)){
            return ResultUtil.fail("字典类型名称已使用");
        }
        // 校验字典类型编码
        existDictType = this.getOne(Wrappers.<DictType>lambdaQuery()
                .eq(DictType::getDictTypeCode, dto.getDictTypeCode())
                .eq(DictType::getIsDelete, DeleteEnum.NOT_DELETE.getStatus()));
        if(Objects.nonNull(existDictType)){
            return ResultUtil.fail("字典类型编码已使用");
        }
        DictType dictType = new DictType();
        BeanUtils.copyProperties(dto, dictType);
        dictType.setIsDelete(DeleteEnum.NOT_DELETE.getStatus());
        dictType.setCreaterId(loginUser.getUserId());
        dictType.setCreateTime(LocalDateTime.now());
        this.save(dictType);
        return ResultUtil.success();
    }

    @Override
    @ParameterCheck
    public Result<String> updateDicType(DictTypeDTO dto) {
        LoginUser loginUser = LoginUserHelper.getLoginUser();
        DictType dictType = this.getDictTypeAssert(dto.getId());
        // 校验字典类型名称
        DictType existDictType = this.getOne(Wrappers.<DictType>lambdaQuery()
                .eq(DictType::getDictTypeName, dto.getDictTypeName())
                .eq(DictType::getIsDelete, DeleteEnum.NOT_DELETE.getStatus())
                .ne(DictType::getId, dto.getId()));
        if(Objects.nonNull(existDictType)){
            return ResultUtil.fail("字典类型名称已使用");
        }
        // 校验字典类型编码
        existDictType = this.getOne(Wrappers.<DictType>lambdaQuery()
                .eq(DictType::getDictTypeCode, dto.getDictTypeCode())
                .eq(DictType::getIsDelete, DeleteEnum.NOT_DELETE.getStatus())
                .ne(DictType::getId, dto.getId()));
        if(Objects.nonNull(existDictType)){
            return ResultUtil.fail("字典类型编码已使用");
        }
        BeanUtils.copyProperties(dto, dictType);
        dictType.setUpdaterId(loginUser.getUserId());
        dictType.setUpdateTime(LocalDateTime.now());
        this.updateById(dictType);
        return ResultUtil.success();
    }

    @Override
    @ParameterCheck
    public Result<String> deleteDicType(@NotNull(message = "参数不能为空") Long dictTypeId) {
        LoginUser loginUser = LoginUserHelper.getLoginUser();
        DictType dictType = this.getDictTypeAssert(dictTypeId);
        dictType.setUpdaterId(loginUser.getUserId());
        dictType.setUpdateTime(LocalDateTime.now());
        dictType.setIsDelete(DeleteEnum.DELETE.getStatus());
        this.updateById(dictType);
        return ResultUtil.success();
    }

    /**
     * 查询字典类型
     *
     * @param dicTypeId 字典类型ID
     * @return DictType
     */
    private DictType getDictTypeAssert(Long dicTypeId) {
        Assert.notNull(dicTypeId, "dicTypeId is null");
        DictType dictType = this.getOne(Wrappers.<DictType>lambdaQuery().eq(DictType::getId, dicTypeId).eq(DictType::getIsDelete, DeleteEnum.NOT_DELETE.getStatus()));
        if (Objects.isNull(dictType)) {
            throw new BaseException(ResultConstants.QUERY_NOT_FOUND_DATA);
        }
        return dictType;
    }
}
